import AjaxForBusOpen from '@/utils/AjaxForBusOpen'
import { OptimizeBusInfoModel } from '../models/OptimizeBusInfoModel'
import { MyReservationModel } from '../models/MyReservationModel'

/**
 * 获取包车全局配置信息
 */
async function getGlobals() {
  let postData = {}
  let res
  res = await AjaxForBusOpen('/chartered-bus-api/chartered/globals', postData)
  return res
}

/**
 * 获取优选好车列表
 * @param {Number} pageCapacity
 * @param {Number} pageIndex
 */
async function getOptimizeBus(pageCapacity, pageIndex) {
  let postData = {
    PAGE_CAPACITY: pageCapacity,
    PAGE_INDEX: pageIndex
  }
  let res
  res = await AjaxForBusOpen(
    '/chartered-bus-api/chartered/optimal/list',
    postData
  )
  let optimizeBusList = []
  if (res.LIST) {
    res.LIST.forEach(item => {
      optimizeBusList.push(
        new OptimizeBusInfoModel(
          item.ID,
          item.BUS_NAME,
          item.MASTER_PIC_URL,
          item.MASTER_PIC_URL_MIN,
          item.AVAILABLE_SEAT,
          item.TOTAL_SEAT
        )
      )
    })
  }
  return optimizeBusList
}

/**
 * 我的预约-列表
 * @param {*} pageCapacity
 * @param {*} pageIndex
 */
async function getMyReservationList(pageIndex, pageCapacity) {
  let postData = {
    PAGE_CAPACITY: pageCapacity,
    PAGE_INDEX: pageIndex
  }
  let res
  res = await AjaxForBusOpen(
    '/chartered-bus-api/chartered/bus_order/list',
    postData
  )
  let myReservationList = []
  if (res.LIST) {
    res.LIST.forEach(item => {
      myReservationList.push(
        new MyReservationModel(
          item.ORDER_NO,
          item.STATE,
          item.STATE_DESC,
          item.STATE_DETAIL_DESC,
          item.TRIP_ADDRESS,
          item.TRIP_START_TIME,
          item.TRIP_END_TIME,
          item.CREATE_TIME
        )
      )
    })
  }
  let myReservationListObj = {
    count: res.COUNT,
    list: myReservationList
  }
  return myReservationListObj
}
export { getGlobals, getOptimizeBus, getMyReservationList }
